{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "compile是指把Sequential中的层序列转成一系列的矩阵。  \n",
    "可以视为precompute。  \n",
    "真正的compute是fit数据的操作。  \n",
    "\n",
    "# 两种定义model.compile的参数的方式\n",
    "\n",
    "方式一：用字符串定义算法并使用默认参数\n",
    "\n",
    "```python\n",
    "model.compile(optimizer='sgd', loss='mse')\n",
    "```\n",
    "\n",
    "方式二：用对象定义，并在创建对象时定义参数  \n",
    "\n",
    "```python\n",
    "algorithm = SGD(lr=0.1, momentum=0.3)\n",
    "model.compile(optimizer=algorithm, loss='mse')\n",
    "```\n",
    "\n",
    "# 损失函数loss\n",
    "\n",
    "回归问题：`mean_squared_error`或`mse`、`mae`  \n",
    "二分类问题：`binary_corssentropy`  \n",
    "多分类问题：`categorical_crossentropy`  '\n",
    "\n",
    "# 优化算法optimizer\n",
    "\n",
    "`sgd`、`adam`、`rmsprop`\n",
    "\n",
    "# 迭代过程中可以收集的统计数据metrics\n",
    "\n",
    "`accuracy`、`acc`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 用正则化定义自己的loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(), dtype=float32, numpy=0.04>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "W = tf.Variable(tf.ones(shape=(2,2)))\n",
    "regularizer = tf.keras.regularizers.l2()\n",
    "reg_loss = regularizer(W)\n",
    "reg_loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(), dtype=float32, numpy=0.04>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "W = tf.Variable(tf.ones(shape=(2,2)))\n",
    "regularizer = tf.keras.regularizers.l1()\n",
    "reg_loss = regularizer(W)\n",
    "reg_loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
